<?php

use yii\db\Migration;

class m161007_021200_add_column_create_time_for_complain extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=myisam';
        }
    
        $this->addColumn('mkt_post_complain', 'create_time', $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'));

        $this->addColumn('mkt_user', 'gh_id', $this->string(32)->notNull()->comment('原始ID')->after('update_time'));
    
        $this->createTable('mkt_wx_gh', [
            'slug' => $this->string(32),                            
            'title' => $this->string(32),                    
            'gh_id' => $this->string(32)->notNull(),
            'appId' => $this->string(64)->notNull(),
            'appSecret' => $this->string(64)->notNull(),
            'token' => $this->string(32)->notNull(),
            'accessToken' => $this->string(512),
            'accessToken_expiresIn' => $this->integer(),
            'encodingAESKey' => $this->string(43),
            'encodingMode' => $this->smallInteger(),
            'created_at' => $this->integer()->notNull()->defaultValue(0),
            'updated_at' => $this->integer()->notNull()->defaultValue(0),
            'jsapiTicket' => $this->string(512),     
            'jsapiTicket_expiresIn' => $this->integer(),            
            'wxPayMchId' => $this->string(32),     
            'wxPayApiKey' => $this->string(64),                 
            'wxcardapiTicket' => $this->string(512),                 
            'wxcardapiTicket_expiresIn' => $this->integer(),            
        ], $tableOptions);
        
        $this->addPrimaryKey('WX_GH_KEY', 'mkt_wx_gh', ['gh_id']);        
        $this->createIndex('idx_slug', 'mkt_wx_gh', ['slug'], true);
        
        $this->insert('mkt_wx_gh', [
            'slug'=>'juyong',
            'title' => '聚用',
            'gh_id' => 'gh_8510652496c4',
            'appId' => 'wx32a608a990280588',
            'appSecret' => '7cc30bd0e2393eda2dad392b83716553',     
            'token' => 'vDHg6heBH3m6OM6F7D3638EObEZEDm3b',
        ]);

        $this->createTable('mkt_wx_menu', [
            'id' => $this->primaryKey(),
            'gh_id' => $this->string(64)->notNull(),
            'name' => $this->string(40)->notNull(),
            'parent_id' => $this->integer(),
            'type' => $this->string(32),
            'key' => $this->string(512),
            'order' => $this->smallInteger(),
        ], $tableOptions);
        
    }

    public function down()
    {

        $this->dropTable('mkt_wx_menu');        

        $this->dropTable('mkt_wx_gh');

        $this->dropColumn('mkt_user', 'gh_id');
        
        $this->dropColumn('mkt_post_complain', 'create_time');
        
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}
